package com.itwc.websocketdemo.mapper;

import com.itwc.websocketdemo.entity.UserPostRelation;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 用户-推文关联表 数据层
 */
@Mapper
public interface UserPostRelationMapper {
    
    /**
     * 批量插入用户推文关联记录
     */
    @Insert("<script>" +
            "INSERT INTO user_post_relation (user_id, post_id, is_read, is_pushed, create_time, update_time) VALUES " +
            "<foreach collection='relations' item='item' separator=','>" +
            "(#{item.userId}, #{item.postId}, #{item.isRead}, #{item.isPushed}, #{item.createTime}, #{item.updateTime})" +
            "</foreach>" +
            "</script>")
    int batchInsertRelations(@Param("relations") List<UserPostRelation> relations);
    
    /**
     * 查询用户未读推文数量
     */
    @Select("SELECT COUNT(*) FROM user_post_relation WHERE user_id = #{userId} AND is_read = 0")
    int countUnreadPosts(@Param("userId") Long userId);
    
    /**
     * 查询用户未推送的推文
     */
    @Select("SELECT upr.*, ap.title, ap.content, ap.summary, ap.author_name, ap.publish_time " +
            "FROM user_post_relation upr " +
            "LEFT JOIN ai_post ap ON upr.post_id = ap.id " +
            "WHERE upr.user_id = #{userId} AND upr.is_pushed = 0 AND ap.status = 1 AND ap.deleted = 0 " +
            "ORDER BY ap.publish_time DESC")
    List<UserPostRelation> selectUnpushedPosts(@Param("userId") Long userId);
    
    /**
     * 查询用户的推文列表（包含未读状态）
     */
    @Select("SELECT upr.id, upr.user_id, upr.post_id, upr.is_read, upr.is_pushed, upr.create_time, upr.update_time, " +
            "ap.title, ap.content, ap.summary, ap.author_name, ap.publish_time, ap.view_count, ap.like_count " +
            "FROM user_post_relation upr " +
            "LEFT JOIN ai_post ap ON upr.post_id = ap.id " +
            "WHERE upr.user_id = #{userId} AND ap.status = 1 AND ap.deleted = 0 " +
            "ORDER BY ap.is_top DESC, ap.sort_order DESC, ap.publish_time DESC")
    List<UserPostRelation> selectUserPostsWithReadStatus(@Param("userId") Long userId);
    
    /**
     * 标记推文为已读
     */
    @Update("UPDATE user_post_relation SET is_read = 1, update_time = NOW() WHERE user_id = #{userId} AND post_id = #{postId}")
    int markAsRead(@Param("userId") Long userId, @Param("postId") Long postId);
    
    /**
     * 标记推文为已推送
     */
    @Update("UPDATE user_post_relation SET is_pushed = 1, update_time = NOW() WHERE user_id = #{userId} AND post_id = #{postId}")
    int markAsPushed(@Param("userId") Long userId, @Param("postId") Long postId);
    
    /**
     * 批量标记推文为已推送
     */
    @Update("<script>" +
            "UPDATE user_post_relation SET is_pushed = 1, update_time = NOW() WHERE " +
            "<foreach collection='postIds' item='postId' open='(' separator=' OR ' close=')'>" +
            "(user_id = #{userId} AND post_id = #{postId})" +
            "</foreach>" +
            "</script>")
    int batchMarkAsPushed(@Param("userId") Long userId, @Param("postIds") List<Long> postIds);
}